package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.DepartmentDao;
import club.banyuan.hrms.domain.Admin;
import club.banyuan.hrms.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class DepartmentServlet extends HttpServlet {
    private DepartmentDao departmentDao;

    @Autowired
    public DepartmentServlet(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    public DepartmentDao getDepartmentDao() {
        return departmentDao;
    }

    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id != null) {
            Department department = departmentDao.getDepartmentById(Integer.parseInt(id));
            if (department != null) {
                // 将model 传入jsp

                request.setAttribute("username", department.getName());
                request.setAttribute("rolename", department.getDescription());
            }
        }
        request.getRequestDispatcher("dept.jsp?id=" + id).forward(request, response);
    }
}
